#!/bin/sh

# this script is used for debugging MPI Application
# > USAGE: ./mpidebug ${MPIRUN_ARGS}  -- ${TARGET}

if [ $# -eq 0 ]; then
    exit 0
fi

flag=0
margs=()
pargs=()
for arg in $@ ; do
    if [ $arg = "--" ]; then
        arg=""
        flag=1
    fi
    if [ $flag -eq 0 ]; then
        margs=(${margs[@]} $arg)
    else
        pargs=(${pargs[@]} $arg)
    fi
done

if [ ${#pargs[@]} -eq 0 ]; then
    echo "mpidebug: no target program"
    exit 0
fi

mpirun ${margs[@]} xterm -e gdb --args ${pargs[@]}
